package com.easylinkin.linkappapi.constructionalbum.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.constructionalbum.entity.ConstructionAlbum;
import com.easylinkin.linkappapi.constructionalbum.entity.vo.ConstructionAlbumInfoVo;
import com.easylinkin.linkappapi.constructionalbum.entity.vo.ConstructionAlbumVo;
import com.easylinkin.linkappapi.constructionalbum.service.ConstructionAlbumService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * ConstructionAlbum表控制层
 *
 * @author zhengwen
 * @date 2022/11/07
 */
@Slf4j
@RestController
@RequestMapping("/constructionAlbum")
public class ConstructionAlbumController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private ConstructionAlbumService constructionAlbumService;

    /**
     * 分页查询所有数据
     *
     * @param requestModel 查询分页对象
     * @return 所有数据
     */
    @PostMapping("getPage")
    @ApiOperation("查询分页")
    public RestMessage selectPage(@RequestBody RequestModel<ConstructionAlbum> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<ConstructionAlbum> record = constructionAlbumService.selectPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder(record).build();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("查询单条")
    public RestMessage selectOne(@PathVariable Serializable id) {
        return RestBuilders.successBuilder((this.constructionAlbumService.getOneById(id))).build();
    }

    /**
     * 新增数据
     *
     * @param appConstructionAlbum 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增")
    public RestMessage insert(@RequestBody ConstructionAlbum appConstructionAlbum) {
        return RestBuilders.successBuilder().success((this.constructionAlbumService.saveOne(appConstructionAlbum))).build();
    }

    /**
     * 修改数据
     *
     * @param appConstructionAlbum 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation("修改单条")
    public RestMessage update(@RequestBody ConstructionAlbum appConstructionAlbum) {
        return RestBuilders.successBuilder().success(this.constructionAlbumService.updateOne(appConstructionAlbum)).build();
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation("批量删除")
    public RestMessage delete(@RequestParam("idList") List<Long> idList) {
        return RestBuilders.successBuilder().success(this.constructionAlbumService.deleteByIds(idList)).build();
    }

    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody ConstructionAlbum appConstructionAlbum, HttpServletRequest request, HttpServletResponse response) {
        constructionAlbumService.export(appConstructionAlbum, request, response);
    }


    /**
     * 新增相册、事件
     *
     * @param constructionAlbumVo 条件vo
     * @return 统一出参
     */
    @PostMapping("/add")
    @ApiOperation("新增")
    public RestMessage add(@RequestBody ConstructionAlbumVo constructionAlbumVo) {
        return constructionAlbumService.add(constructionAlbumVo);
    }

    /**
     * 编辑相册、事件
     *
     * @param constructionAlbumVo 条件vo
     * @return 统一出参
     */
    @PutMapping("/update")
    @ApiOperation("编辑")
    public RestMessage update(@RequestBody ConstructionAlbumVo constructionAlbumVo) {
        return constructionAlbumService.update(constructionAlbumVo);
    }

    /**
     * 分页查询所有数据dto
     *
     * @param requestModel 查询分页对象
     * @return 所有数据
     */
    @PostMapping("/getDtoPage")
    @ApiOperation("查询分页Dto")
    public RestMessage getDtoPage(@RequestBody RequestModel<ConstructionAlbumVo> requestModel) {
        return constructionAlbumService.getDtoPage(requestModel);
    }

    /**
     * 批量删除，级联更新子孙状态
     *
     * @param idList 删除id
     * @return 统一出参
     */
    @DeleteMapping("/batchDelete")
    @ApiOperation("批量删除")
    public RestMessage batchDelete(@RequestParam("idList") List<Long> idList) {
        return constructionAlbumService.batchDelete(idList);
    }

    /**
     * 保存事件图片
     *
     * @param constructionAlbumInfoVo 事件图片vo
     * @return 统一出参
     */
    @PostMapping("/saveEventImgs")
    @ApiOperation("保存事件图片")
    public RestMessage saveEventImgs(@RequestBody ConstructionAlbumInfoVo constructionAlbumInfoVo) {
        return constructionAlbumService.saveEventImgs(constructionAlbumInfoVo);
    }

    /**
     * 事件详情dto
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/eventDetail/{id}")
    @ApiOperation("事件详情dto")
    public RestMessage eventDetail(@PathVariable Serializable id) {
        cn.hutool.core.lang.Assert.isTrue(id != null, "id为空");
        return constructionAlbumService.eventDetail(Long.parseLong(id.toString()));
    }

    /**
     * app自定义相册导出
     *
     * @param constructionAlbumInfoVo 入参
     * @return 统一出参
     */
    @PostMapping("/exportAppAlbum")
    @ApiOperation("app自定义相册导出")
    public RestMessage exportAppAlbum(@RequestBody ConstructionAlbumInfoVo constructionAlbumInfoVo) {
        cn.hutool.core.lang.Assert.isTrue(StringUtils.isNotBlank(constructionAlbumInfoVo.getIds()), "选择不能为空");

        String fileName = constructionAlbumService.exportAppAlbum(constructionAlbumInfoVo);
        return RestBuilders.successBuilder().data(fileName).build();
    }

}

